//
// Created by vcq on 21/3/31.
//
#pragma once

#include <string>
#include <cstdlib>
#include <model/user.hpp>
#include <model/_user_manager.hpp>
#include "model/plan.hpp"
#include "model/finishrequire.hpp"
#include "model/_plan_goal.hpp"
#include "model/_require_syllabus.hpp"
#include "model/profession.hpp"
#include "model/require.hpp"
#include "model/grade.hpp"
#include "model/syllabus.hpp"

using namespace nlohmann;

static void init_session(request &req, response &res, const std::string &user_id) {
    auto session = req.get_session().lock();
    if (session == nullptr) {
        auto new_session = res.start_session();
        new_session->set_data("user_name", user_id);
        new_session->set_max_age(-1);
    } else {
        if (session->get_data<std::string>("user_name").empty()) {
            session->set_data("user_name", user_id);
        }
    }
}
void quit(request& req, response& res) {
    auto ptr = req.get_session();
    auto session = ptr.lock();
    if (session) {
        session->set_max_age(0);
    }

    res.redirect("./home");
}
static void login(request &req, response &res) {
    basic_string_view<char> user_name = req.get_query_value("username");
    basic_string_view<char> password = req.get_query_value("password");
    string sql = "select * from user where user_login='";
    sql += user_name;
    sql += "' and user_pass=md5('";
    sql += password;
    sql += "')";
    Dao dao;
    auto r = dao.query<user>(sql);
    if (r.empty()) {
        res.set_status_and_content(status_type::bad_request, "user name or password is not right");
        return;
    }
    std::string user_id = r[0].user_login;
    init_session(req, res, user_id);
    res.set_status_and_content(status_type::ok);
}
static bool check_login(request &req, response &res) {
    auto session=req.get_session().lock();
    if (session == nullptr) {
        res.set_status_and_content(status_type::forbidden, "not login");
        return false;
    }
    auto login_user_name = session->get_data<string>("user_name");
    string sql = "select * from user where user_login = '";
    sql += login_user_name + "'";
    Dao dao;
    auto r = dao.query<user>(sql);
    if (r.empty()) {
        res.set_status_and_content(status_type::forbidden, "not login");
        return false;
    }
    return true;
}

static int get_user_id(request &req, response &res) {
    auto session = req.get_session().lock();
    if (session == nullptr) {
        res.set_status_and_content(status_type::forbidden, "not login");
        return 0;
    }
    auto login_user_name = session->get_data<string>("user_name");
    string sql = "select * from user where user_login = '";
    sql += login_user_name + "'";
    Dao dao;
    auto r = dao.query<user>(sql);
    if (r.empty()) {
        res.set_status_and_content(status_type::forbidden, "not login");
        return 0;
    }
    for (auto &o : r) {
        return o.id;
    }
}

vector<string> check_syllabus(request &req, response &res) {
    auto session = req.get_session().lock();
    vector<string> ret;
    if (session == nullptr) {
        res.set_status_and_content(status_type::forbidden, "not login");
        return ret;
    }
    auto login_user_name = session->get_data<string>("user_name");
    string sql = "select * from _user_manager where manager_type = 'syllabus' and user_id= (select id from user where user_login='";
    sql += login_user_name + "')";
    Dao dao;
    auto r = dao.query<_user_manager>(sql);
    if (r.empty()) {
        return ret;
    }
    for (auto &o : r) {
        ret.push_back(to_string(o.manager_id));
    }
    return ret;
}

static void login_now(request &req, response &res) {
    auto session = req.get_session().lock();
    json ret;
    if (session == nullptr) {
        ret["user_name"] = "not login";
        res.set_status_and_content(status_type::ok, ret.dump(), req_content_type::json);
        return;
    }
    auto login_user_name = session->get_data<string>("user_name");
    string sql = "select * from user where user_login = '";
    sql+=login_user_name+"'";
    Dao dao;
    auto r = dao.query<user>(sql);
    if (r.empty()) {
        ret["user_name"]="not login";
        res.set_status_and_content(status_type::ok,ret.dump(),req_content_type::json);
        return;
    }
    ret["user_name"]=r[0].user_nickname;
    res.set_status_and_content(status_type::ok, ret.dump(),req_content_type::json);
}